'''
$Revision: 124 $
$LastChangedDate: 2009-05-06 14:29:18 +0000 (Wed, 06 May 2009) $
Testing delta dict
'''
from __future__ import division
import unittest
import os, sys

from MomentClosure.GetReactionInformation import GetStoichiometryDict
from MomentClosure.VariableDict import Variables
from sympy import Rational, Symbol, simplify

class TestReactionParsing(unittest.TestCase):
    
    
    def setUp(self):
        pass

    
    def testZeroOrder(self):
        Variables().initialise()
        gdd = GetStoichiometryDict(sbml_file = 'xml_files/test/zero_order_reactions.xml')
        
        reaction_list = gdd.getReactionList()
        
        c1 = Symbol('c1')
        c2 = Symbol('c2')
        c3 = Symbol('c3')
        c4 = Symbol('c4')
        c5 = Symbol('c5')

        for value in reaction_list:
            if value.sbml_id == 'R1':
                self.assertEqual([-1,0,0], value.stoic)
                diff = value.alpha/value.beta - c1
                self.assertEqual(diff, 0)
            elif value.sbml_id == 'R2':
                self.assertEqual([-1,0,0], value.stoic)
                diff = value.alpha/value.beta - (c1-c2)
                self.assertEqual(diff, 0)
            elif value.sbml_id == 'R3':
                self.assertEqual([-1,0,0], value.stoic)
                diff = value.alpha/value.beta - c3*(c1-c2)
                self.assertEqual(diff, 0)
            elif value.sbml_id == 'R4':
                self.assertEqual([-1,0,0], value.stoic)
                diff = value.alpha/value.beta - (c3*c1-c3*c2)/c4
                diff = diff.map_coeffs(simplify) 
                self.assertEqual(diff, 0)
            elif value.sbml_id == 'R5':
                self.assertEqual([-1,0,0], value.stoic)
               
                diff = value.alpha/value.beta - c3*(c1-c2)/(c4/2)
                diff = diff.map_coeffs(simplify)
                self.assertEqual(diff, 0)
            else:
                print value.sbml_id
                raise 'Unknown reaction'
                
    def testFirstOrder(self):
        Variables().initialise()
        gdd = GetStoichiometryDict(sbml_file = 'xml_files/test/first_order_reactions.xml')
        
        reaction_list = gdd.getReactionList()
        
        c1 = Symbol('c1')
        c2 = Symbol('c2')
        c3 = Symbol('c3')
        c4 = Symbol('c4')
        c5 = Symbol('c5')
        X, Y = Symbol('X'), Symbol('Y')

        for value in reaction_list:
            if value.sbml_id == 'R1':
                self.assertEqual([0,0,0], value.stoic)
                self.assertEqual({(1, 0, 0): c1}, value.alpha/value.beta)
            elif value.sbml_id == 'R2':
                self.assertEqual([-1,1,1], value.stoic)
                self.assertEqual({(1, 0, 0): c1-c2}, value.alpha/value.beta)    
            elif value.sbml_id == 'R3':
                self.assertEqual([-1,0,1], value.stoic)
                diff = value.alpha/value.beta- c3*(c1-c2)*X
                diff = diff.map_coeffs(simplify) 
                self.assertEqual(diff, 0)
            elif value.sbml_id == 'R4':
                self.assertEqual([-1,0,0], value.stoic)
                diff = value.alpha/value.beta - c3*c1/c4*X + c3*c2/c4
                diff = diff.map_coeffs(simplify) 
                self.assertEqual(diff, 0)
            elif value.sbml_id == 'R5':
                self.assertEqual([0,-1,0], value.stoic)
                diff = value.alpha/value.beta + c3*c2/(c4/2) - c3*c1/(c4/2)*Y
                diff = diff.map_coeffs(simplify) 
                self.assertEqual(diff, 0)
            else:
                print value.sbml_id
                raise 'Unknown reaction'
                
    def testSecondOrder(self):
        Variables().initialise()
        gdd = GetStoichiometryDict(sbml_file = 'xml_files/test/second_order_reactions.xml')
        
        reaction_list = gdd.getReactionList()
        
        c1 = Symbol('c1')
        c2 = Symbol('c2')
        c3 = Symbol('c3')
        c4 = Symbol('c4')
        c5 = Symbol('c5')
        X, Y, Z = Symbol('X'), Symbol('Y'), Symbol('Z')
        
        for value in reaction_list:
            if value.sbml_id == 'R1':
                self.assertEqual([0,-1,0], value.stoic)
                diff = value.alpha/value.beta- c1*X*Y
                self.assertEqual(diff, 0)
            elif value.sbml_id == 'R2':
                self.assertEqual([-1,1,0], value.stoic)
                diff = value.alpha/value.beta - c1*X + c2*X*Z
                self.assertEqual(diff, 0)
            elif value.sbml_id == 'R3':
                self.assertEqual([1,0,0], value.stoic)
                diff = value.alpha/value.beta - c1 + c3*c2*X/2 - c3*X*X/2
                self.assertEqual(diff, 0)
            elif value.sbml_id == 'R4':
                self.assertEqual([-1,0,-1], value.stoic)
                diff = value.alpha/value.beta - c3*c1*X*Z/c4 + c3*c2*Z/c4
                self.assertEqual(diff, 0)
            elif value.sbml_id == 'R5': 
                self.assertEqual([0,-1,0], value.stoic)
                diff = value.alpha/value.beta - Y*Y* c3*c1*c1/(c4/2) + Y*(c3*c1*c2+c2*c1*c3)/(c4/2) - c3*c2*c2/(c4/2)                
                self.assertEqual(diff.expand(), 0)
            else:
                print value.sbml_id
                raise 'Unknown reaction'
        
 
if __name__ == '__main__':
    unittest.main()









#ch07-dimer-stoch.xml
#LV2.xml                               
#ch07-mm-det.xml
#imdeath.xml                      
#immigration_migration_model.xml
#sis.xml
